package com.wlts.wzy.DYOrderModule.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.linkedmall.model.v20180116.QueryLogisticsRequest;
import com.aliyuncs.linkedmall.model.v20180116.QueryLogisticsResponse;
import com.wlts.wzy.DYOrderModule.entity.DyOrders;
import com.wlts.wzy.DYOrderModule.service.IDyOrdersService;
import com.wlts.wzy.DYUserModule.entity.DyStoreUser;
import com.wlts.wzy.DYUserModule.service.IDyStoreUserService;
import com.wlts.wzy.api.vo.DyVo.ExpressName;
import com.wlts.wzy.configBean.LmConfig.LmConfig;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.wlts.wzy.configBean.LmConfig.LmConfig.client;

@Component
public class DyLmExpressToLocalUtil {
    @Autowired
    private IDyOrdersService ksOrdersService;
    @Autowired
    private IDyStoreUserService ksStoreUserService;

    /**
     * 获得发货订单的快递单号
     */
    @Scheduled(fixedDelay = 120 * 1000)
    public void LmUpdateDyOrdersToLocal(){


        System.out.println(" 获得发货抖音订单的快递单号 计时器");

        MyLambdaQueryWrapper<DyStoreUser> userWrapper = new MyLambdaQueryWrapper<>();

        userWrapper.eq(DyStoreUser::getDelFlag, "0");

        userWrapper.isNotNull(DyStoreUser::getDySellerId);

        List<DyStoreUser> ksOauthList = ksStoreUserService.list(userWrapper);

        for (DyStoreUser user : ksOauthList
        ) {
            MyLambdaQueryWrapper<DyOrders> queryWrapper = new MyLambdaQueryWrapper<>();

            queryWrapper.in(DyOrders::getSelfOrder, 1);//平台商品的订单 0未确定 1是 2不是

            queryWrapper.eq(DyOrders::getPutLmStatus, 1);//推送lm状态 0未推送 1已推送 2推送失败

            queryWrapper.eq(DyOrders::getSendStatus, 1);//lm发货状态 0发货 1已发货

          //  queryWrapper.eq(DyOrders::getFinalStatus, 2);

            queryWrapper.isNull(DyOrders::getLogisticsCode);

            queryWrapper.eq(DyOrders::getShopId, user.getDySellerId());

            queryWrapper.isNotNull(DyOrders::getLmOrderId);

            int page = 1;
            int pageSize = 15;

            while (true) {

                List<DyOrders> ksOrdersList = ksOrdersService.page(queryWrapper, page, pageSize, true).getRecords();
//                System.out.println("这是需要更新发货信息的订单-----");
//                System.out.println(ksOrdersList.toString());
//                System.out.println("这是需要更新发货信息的订单-----");
                ksOrdersList.forEach(ksOrders -> {
                    QueryLogisticsRequest request = new QueryLogisticsRequest();
                    request.setBizId(LmConfig.LZ_BIZ_ID);
                    request.setAccountType("ANONY");
                    request.setThirdPartyUserId(user.getDySellerId() + "");
                    request.setBizUid(request.getThirdPartyUserId());
                    request.setLmOrderId(Long.valueOf(ksOrders.getLmOrderId()));

                    try {
                        QueryLogisticsResponse response = client.getAcsResponse(request);
//                        System.out.println("更新订单快递信息状态");
//                        System.out.println(JSON.toJSONString(response));


                        if (response.getCode().equals("SUCCESS")) {
                            List<QueryLogisticsResponse.DataItem> data = response.getData();
                            QueryLogisticsResponse.DataItem item = data.get(0);
                            if (item != null) {
                                ksOrders.setExpressNo(item.getMailNo());
                                ksOrders.setExpressName( item.getLogisticsCompanyName());
                            //    DyExpressCode expressCode = ksExpressCodeService.getOne(new MyLambdaQueryWrapper<DyExpressCode>().like(DyExpressCode::getName, item.getLogisticsCompanyName()));

                                List<ExpressName> expressNameList=  DyExpressUtils.likeString(item.getLogisticsCompanyName());
                                if (expressNameList.size()> 0) {
                                    ksOrders.setExpressCode(expressNameList.get(0).getId());
                                }
                            }
                            ksOrdersService.updateById(ksOrders);
                        }
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                });
                if (ksOrdersList.size() < pageSize) {
                    break;
                }
                page += 1;
            }
        }

    }


}
